<template>
  <scroll-view
    class="list"
    :style="{
      height: '90vh',
    }"
    scroll-y
  >
    <!-- 隐患详情 S -->
    <view class="info-box">
      <t-cell-group bordered>
        <t-cell title="单位名称" :note="info.enterpriseName || '--'" />
        <t-cell title="处罚内容" :description="info.content || '--'" />

        <t-cell title="行政处罚时间" :note="info.punishTime || '--'" />

        <t-cell title="附件">
          <template #description>
            <view class="file-list">
              <view
                v-for="(item, index) in fileList"
                :key="index"
                class="file-item"
                @click="previewFile(item)"
              >
                {{ item.originalName }}</view
              >
            </view>
          </template>
        </t-cell>
      </t-cell-group>
    </view>
    <!-- 隐患详情 E -->
  </scroll-view>
</template>

<script setup>
import { ref, nextTick, getCurrentInstance, computed } from "vue";
import { onLoad, onShow } from "@dcloudio/uni-app";
import { getAdminPunishmentAttachment } from "@/api/administrativePenalty";
import { downloadFileUrl } from "@/api/common";

const { proxy } = getCurrentInstance();

const info = ref({});
const fileList = ref([]);

onLoad(async () => {
  await nextTick();
  const eventChannel = proxy.getOpenerEventChannel();
  eventChannel.on("sendInfo", (data) => {
    //data:接收到的数据
    console.log(data);
    info.value = data.info;

    getFiles();
  });
});

const getFiles = () => {
  getAdminPunishmentAttachment(info.value.id)
    .then((res) => {
    //   console.log("附件列表：", res);
      fileList.value = res.data;
    })
    .catch((err) => {
      console.log("附件列表错误：", err);
    });
};

// 预览附件
const previewFile = (item) => {
  uni.downloadFile({
    url: downloadFileUrl + item.fileUrl,
    success: (res) => {
    //   console.log("下载成功：", res);
      uni.openDocument({
        filePath: res.tempFilePath,
        success: (res) => {
          console.log("打开文档成功：", res);
        },
        fail: (err) => {
          console.log("打开文档失败：", err);
        },
      });
    },
    fail: (err) => {
      console.log("下载失败：", err);
    },
  });
};
</script>

<style lang="less" scoped>
.list {
}

.img-list {
  display: flex;
  flex-wrap: wrap;

  .image-container {
    margin-right: 10px;
    margin-top: 10px;
  }
}

.file-list {
  display: flex;
  flex-wrap: wrap;
  padding: 20rpx;
}
.file-item {
  margin-top: 10px;
  color: #1989fa;
  font-size: 30rpx;
}
</style>
